home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
X User Tools
/
X User Tools (O'Reilly and Associates)(1994).ISO
/
sun4c
/
archive
/
tcltk.z
/
tcltk
/
man
/
cat3
/
CrtCommand.3
< prev
next >
Wrap
Text File
|
1994-09-20
|
10KB
|
265 lines
Tcl_CreateCommand(3) Tcl Library Procedures
_________________________________________________________________
NAME
Tcl_CreateCommand, Tcl_DeleteCommand, Tcl_GetCommandInfo,
Tcl_SetCommandInfo - implement new commands in C
SYNOPSIS
#include <tcl.h>
Tcl_CreateCommand(_i_n_t_e_r_p, _c_m_d_N_a_m_e, _p_r_o_c, _c_l_i_e_n_t_D_a_t_a, _d_e_l_e_t_e_P_r_o_c)
int
Tcl_DeleteCommand(_i_n_t_e_r_p, _c_m_d_N_a_m_e)
int |
Tcl_GetCommandInfo(_i_n_t_e_r_p, _c_m_d_N_a_m_e, _i_n_f_o_P_t_r) |
int |
Tcl_SetCommandInfo(_i_n_t_e_r_p, _c_m_d_N_a_m_e, _i_n_f_o_P_t_r) |
ARGUMENTS
Tcl_Interp *_i_n_t_e_r_p (in) Interpreter
in which to
create new
command.
char *_c_m_d_N_a_m_e (in) Name of com-
mand.
Tcl_CmdProc *_p_r_o_c (in) Implementation
of new com-
mand: _p_r_o_c
will be
called when-
ever _c_m_d_N_a_m_e
is invoked
as a com-
mand.
ClientData _c_l_i_e_n_t_D_a_t_a (in) Arbitrary
one-word
value to
pass to _p_r_o_c
and
_d_e_l_e_t_e_P_r_o_c.
Tcl_CmdDeleteProc *_d_e_l_e_t_e_P_r_o_c (in) Procedure to
call before
_c_m_d_N_a_m_e is
deleted from
the inter-
preter;
Tcl 1
Tcl_CreateCommand(3) Tcl Library Procedures
allows for
command-
specific
cleanup. If
NULL, then
no procedure
is called
before the
command is
deleted.
Tcl_CmdInfo *_i_n_f_o_P_t_r (in/out) Pointer to |
structure |
containing |
various |
information |
about a Tcl |
command.
_________________________________________________________________
DESCRIPTION
Tcl_CreateCommand defines a new command in _i_n_t_e_r_p and asso-
ciates it with procedure _p_r_o_c such that whenever _c_m_d_N_a_m_e is
invoked as a Tcl command (via a call to Tcl_Eval) the Tcl
interpreter will call _p_r_o_c to process the command. If there
is already a command _c_m_d_N_a_m_e associated with the inter-
preter, it is deleted. _P_r_o_c should have arguments and
result that match the type Tcl_CmdProc:
typedef int Tcl_CmdProc(
ClientData _c_l_i_e_n_t_D_a_t_a,
Tcl_Interp *_i_n_t_e_r_p,
int _a_r_g_c,
char *_a_r_g_v[]);
When _p_r_o_c is invoked the _c_l_i_e_n_t_D_a_t_a and _i_n_t_e_r_p parameters
will be copies of the _c_l_i_e_n_t_D_a_t_a and _i_n_t_e_r_p arguments given
to Tcl_CreateCommand. Typically, _c_l_i_e_n_t_D_a_t_a points to an
application-specific data structure that describes what to
do when the command procedure is invoked. _A_r_g_c and _a_r_g_v
describe the arguments to the command, _a_r_g_c giving the
number of arguments (including the command name) and _a_r_g_v
giving the values of the arguments as strings. The _a_r_g_v
array will contain _a_r_g_c+1 values; the first _a_r_g_c values
point to the argument strings, and the last value is NULL.
_P_r_o_c must return an integer code that is either TCL_OK,
TCL_ERROR, TCL_RETURN, TCL_BREAK, or TCL_CONTINUE. See the
Tcl overview man page for details on what these codes mean.
Most normal commands will only return TCL_OK or TCL_ERROR.
In addition, _p_r_o_c must set _i_n_t_e_r_p->_r_e_s_u_l_t to point to a
string value; in the case of a TCL_OK return code this gives
the result of the command, and in the case of TCL_ERROR it
Tcl 2
Tcl_CreateCommand(3) Tcl Library Procedures
gives an error message. The Tcl_SetResult procedure pro-
vides an easy interface for setting the return value; for
complete details on how the _i_n_t_e_r_p->_r_e_s_u_l_t field is managed,
see the Tcl_Interp man page. Before invoking a command pro-
cedure, Tcl_Eval sets _i_n_t_e_r_p->_r_e_s_u_l_t to point to an empty
string, so simple commands can return an empty result by
doing nothing at all.
The contents of the _a_r_g_v array belong to Tcl and are not |
guaranteed to persist once _p_r_o_c returns: _p_r_o_c should not |
modify them, nor should it set _i_n_t_e_r_p->_r_e_s_u_l_t to point any- |
where within the _a_r_g_v values. Call Tcl_SetResult with |
status TCL_VOLATILE if you want to return something from the |
_a_r_g_v array.
_D_e_l_e_t_e_P_r_o_c will be invoked when (if) _c_m_d_N_a_m_e is deleted.
This can occur through a call to Tcl_DeleteCommand or
Tcl_DeleteInterp, or by replacing _c_m_d_N_a_m_e in another call to
Tcl_CreateCommand. _D_e_l_e_t_e_P_r_o_c is invoked before the command
is deleted, and gives the application an opportunity to
release any structures associated with the command.
_D_e_l_e_t_e_P_r_o_c should have arguments and result that match the
type Tcl_CmdDeleteProc:
typedef void Tcl_CmdDeleteProc(ClientData _c_l_i_e_n_t_D_a_t_a);
The _c_l_i_e_n_t_D_a_t_a argument will be the same as the _c_l_i_e_n_t_D_a_t_a
argument passed to Tcl_CreateCommand.
Tcl_DeleteCommand deletes a command from a command inter-
preter. Once the call completes, attempts to invoke _c_m_d_N_a_m_e
in _i_n_t_e_r_p will result in errors. If _c_m_d_N_a_m_e isn't bound as
a command in _i_n_t_e_r_p then Tcl_DeleteCommand does nothing and
returns -1; otherwise it returns 0. There are no restric-
tions on _c_m_d_N_a_m_e: it may refer to a built-in command, an
application-specific command, or a Tcl procedure.
Tcl_GetCommandInfo checks to see whether its _c_m_d_N_a_m_e argu- |
ment exists as a command in _i_n_t_e_r_p. If not then it returns |
0. Otherwise it places information about the command in the |
structure pointed to by _i_n_f_o_P_t_r and returns 1. Tcl_CmdInfo |
structures have fields named _p_r_o_c, _c_l_i_e_n_t_D_a_t_a, and |
_d_e_l_e_t_e_P_r_o_c, which have the same meaning as the corresponding |
arguments to Tcl_CreateCommand. There is also a field |
_d_e_l_e_t_e_D_a_t_a, which is the ClientData value to pass to |
_d_e_l_e_t_e_P_r_o_c; it is normally the same as _c_l_i_e_n_t_D_a_t_a but may |
be set independently using the Tcl_SetCommandInfo procedure. |
Tcl_SetCommandInfo is used to modify the procedures and |
ClientData values associated with a command. Its _c_m_d_N_a_m_e |
argument is the name of a command in _i_n_t_e_r_p. If this com- |
mand exists then Tcl_SetCommandInfo returns 0. Otherwise, |
Tcl 3
Tcl_CreateCommand(3) Tcl Library Procedures
it copies the information from *_i_n_f_o_P_t_r to Tcl's internal |
structure for the command and returns 1. Note that this |
procedure allows the ClientData for a command's deletion |
procedure to be given a different value than the ClientData |
for its command procedure.
KEYWORDS
bind, command, create, delete, interpreter
Tcl 4